#include "gamescreen.h"
#include <QPainter>
#include <QMessageBox>
#include <QFont>

GameScreen::GameScreen(QWidget *parent) : QWidget(parent), gameOver(false), speedUp(false)
{
    setFixedSize(1094, 656);
    setFocusPolicy(Qt::StrongFocus);

    // 加载背景图片
    background.load(":/new/prefix1/image/b2.jpg");

    // 初始化游戏
    game.initialize();

    // 设置游戏定时器
    gameTimer = new QTimer(this);
    connect(gameTimer, &QTimer::timeout, this, &GameScreen::updateGame);
    gameTimer->start(100); // 初始速度
}

GameScreen::~GameScreen()
{
    delete gameTimer;
}

void GameScreen::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setPen(QColor(220, 220, 220));
    int cellSize = 20;

    // 绘制网格线
    painter.drawPixmap(0, 0, width(), height(), background);
    for(int x=0; x<width(); x+=cellSize)
        painter.drawLine(x, 0, x, height());
    for(int y=0; y<height(); y+=cellSize)
        painter.drawLine(0, y, width(), y);
    if (!gameOver)
    {
        // 绘制食物
        drawFood(painter);

        // 绘制蛇
        drawSnake(painter);
    }
    else
    {
        showGameOver(painter);
    }
}

void GameScreen::keyPressEvent(QKeyEvent *event)
{
    if (gameOver)
    {
        if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
        {
            game.initialize();
            gameOver = false;
            gameTimer->start(100);
            update();
        }
        return;
    }

    switch (event->key())
    {
    case Qt::Key_W:
        game.setDirection(Direction::Up);
        break;
    case Qt::Key_A:
        game.setDirection(Direction::Left);
        break;
    case Qt::Key_S:
        game.setDirection(Direction::Down);
        break;
    case Qt::Key_D:
        game.setDirection(Direction::Right);
        break;
    case Qt::Key_Shift:
        speedUp = true;
        gameTimer->setInterval(50); // 加速
        break;
    default:
        QWidget::keyPressEvent(event);
    }
}

void GameScreen::keyReleaseEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Shift)
    {
        speedUp = false;
        gameTimer->setInterval(100); // 恢复正常速度
    }
}

void GameScreen::updateGame()
{
    if (!gameOver)
    {
        game.update();
        if (game.isGameOver())
        {
            gameOver = true;
            gameTimer->stop();
        }
        update();
    }
} //更新图像

void GameScreen::drawSnake(QPainter &painter)
{
    const auto &snakeBody = game.getSnake();
    QColor snakeColor(255, 105, 180); // 设置蛇身为粉色

    for (const auto &segment : snakeBody)
    {
        painter.setBrush(snakeColor);
        painter.setPen(Qt::NoPen);
        painter.drawRect(segment.x() * 20, segment.y() * 20, 20, 20);
    }
}

void GameScreen::drawFood(QPainter &painter)
{
    const auto &food = game.getFood();

    switch (food.type)
    {
    case FoodType::Yellow:
        painter.setBrush(Qt::yellow);
        break;
    case FoodType::Blue:
        painter.setBrush(Qt::blue);
        break;
    case FoodType::Red:
        painter.setBrush(Qt::red);
        break;
    }

    painter.setPen(Qt::NoPen);
    painter.drawEllipse(food.position.x() * 20, food.position.y() * 20, 20, 20);
}

void GameScreen::showGameOver(QPainter &painter)
{
    painter.setPen(Qt::white);
    QFont font("Arial", 36, QFont::Bold);
    painter.setFont(font);

    QString gameOverText = "游戏结束! 得分: " + QString::number(game.getScore());
    painter.drawText(rect(), Qt::AlignCenter, gameOverText);

    font.setPointSize(24);
    painter.setFont(font);
    painter.drawText(rect().adjusted(0, 120, 0, 0), Qt::AlignCenter, "按回车键重新开始");
}
